package io.renren.modules.iots.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.iots.entity.IotsIoserverInfoEntity;
import io.renren.modules.iots.service.IotsIoserverInfoService;
import io.renren.socket.client.TestConnClient;

/**
 * IOServer信息表
 *
 * @author lfy.xys
 * @email liufengyuan@hengtaiboyuan.com
 * @date 2018-06-11 16:42:09
 */
@RestController
@RequestMapping("iots/iotsioserverinfo")
public class IotsIoserverInfoController {
	@Autowired
	private IotsIoserverInfoService iotsIoserverInfoService;

	/**
	 * 列表
	 */
	@RequestMapping("/public/list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = iotsIoserverInfoService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 根据id查询 某一条信息
	 */
	@RequestMapping("/public/selectById")
	public R selectById(@RequestParam Map<String, String> params) {
		Long id = Long.parseLong(params.get("id"));
		IotsIoserverInfoEntity iotsIoserverInfo = iotsIoserverInfoService.selectById(id);
		return R.ok().put("entity", iotsIoserverInfo);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/public/save")
	public R save(@RequestBody IotsIoserverInfoEntity iotsIoserverInfo) {
		iotsIoserverInfoService.insert(iotsIoserverInfo);
		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/public/update")
	public R update(@RequestBody IotsIoserverInfoEntity iotsIoserverInfo) {
		ValidatorUtils.validateEntity(iotsIoserverInfo);
		iotsIoserverInfoService.updateAllColumnById(iotsIoserverInfo);// 全部更新
		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/public/delete")
	public R delete(@RequestParam Map<String, String> params) {
		String idStr = params.get("ids");
		String[] ids = idStr.split(",");
		iotsIoserverInfoService.deleteBatchIds(Arrays.asList(ids));
		return R.ok();
	}
	
	/**
	 * 根据 分组的id ，查询 子IOServer 的数据 。传入参数为pid
	 * 
	 * @author lfy.xys
	 * @date 2018年6月19日
	 *
	 * @param params
	 * @return
	 */
	@RequestMapping("/public/queryIOServerForPid")
	public R queryIOServerForPid(@RequestParam Map<String, String> params) {
		
		Long pid = Long.parseLong((String) params.get("pid"));
		
		List<IotsIoserverInfoEntity> list = iotsIoserverInfoService.queryIOServerForPid(pid);
		return R.ok().put("list", list);
	}
	
	/**
	 * 测试连接
	 * 
	 * http://localhost:8082/iots/iotsioserverinfo/public/testConn
	 * 
	 * @author lfy.xys
	 * @date 2018年6月20日
	 *
	 * @param args
	 */
	@RequestMapping("/public/testConn")
	public R testConn(@RequestParam Map<String, String> params) {
		String ip = params.get("ip");
		Integer port = Integer.parseInt(params.get("port"));
		TestConnClient tcc = new TestConnClient(ip, port);
		return tcc.testConn();
	}

	/**
	 * 同步 IOServer 的设备和变量
	 * 
	 * http://localhost:8082/iots/iotsioserverinfo/public/syncEqmAndVar
	 * 
	 * @author lfy.xys
	 * @date 2018年6月20日
	 *
	 * @param args
	 */
	@RequestMapping("/public/syncEqmAndVar")
	public R syncEqmAndVar(@RequestParam Map<String, String> params) {
		return iotsIoserverInfoService.syncEqmAndVar(params);
	}
	
	
}
